/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pl.tom.social.dal.entity;

import java.io.Serializable;
import java.util.Date;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

/**
 *
 * @author Tom
 */
@Entity
@Table(name = "SiteEntry")
@NamedQueries({@NamedQuery(name = "SiteEntry.countAll", query = "SELECT COUNT(s.id) FROM SiteEntry s"),
	@NamedQuery(name = "SiteEntry.countAllBySite", query = "SELECT COUNT(s.id) FROM SiteEntry s WHERE s.siteId = :site"),
	@NamedQuery(name = "SiteEntry.findIdsBySite", query = "SELECT s.id FROM SiteEntry s WHERE s.siteId = :site"),
	@NamedQuery(name = "SiteEntry.findById", query = "SELECT s FROM SiteEntry s WHERE s.id = :id"),
	@NamedQuery(name = "SiteEntry.findByUrl", query = "SELECT s FROM SiteEntry s WHERE s.url = :url"),
	@NamedQuery(name = "SiteEntry.findBySite",
	query = "SELECT s FROM SiteEntry s WHERE s.siteId = :site ORDER BY s.added")})
public class SiteEntry implements Serializable {

	private static final long serialVersionUID = 1L;
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Basic(optional = false)
	@Column(name = "id")
	private Integer id;
	@Basic(optional = false)
	@Column(name = "url")
	private String url;
	@Column(name = "title")
	private String title;
	@Column(name = "type")
	private Integer type;
	@Basic(optional = false)
	@Column(name = "added")
	@Temporal(TemporalType.TIMESTAMP)
	private Date added;
	@Column(name = "body")
	@Basic(fetch = FetchType.LAZY)
	@Lob
	private String body;
//	@OneToMany(cascade = CascadeType.ALL, mappedBy = "siteEntryid")
//	private Collection<SiteEntryComment> siteEntryCommentCollection;
	@JoinColumn(name = "site_id", referencedColumnName = "id")
	@ManyToOne(optional = false)
	private Site siteId;

	public SiteEntry() {
	}

	public SiteEntry(String url, String title, Integer type, Date added, String body, Site siteId) {
		this.url = url.toLowerCase();
		this.title = title;
		this.type = type;
		this.added = added;
		this.body = body;
		this.siteId = siteId;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url.toLowerCase();
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public Integer getType() {
		return type;
	}

	public void setType(Integer type) {
		this.type = type;
	}

	public Date getAdded() {
		return added;
	}

	public void setAdded(Date added) {
		this.added = added;
	}

	public String getBody() {
		return body;
	}

	public void setBody(String body) {
		this.body = body;
	}

	public Site getSiteId() {
		return siteId;
	}

	public void setSiteId(Site siteId) {
		this.siteId = siteId;
	}

	@Override
	public int hashCode() {
		int hash = 0;
		hash += (id != null ? id.hashCode() : 0);
		return hash;
	}

	@Override
	public boolean equals(Object object) {
		if(!(object instanceof SiteEntry)) {
			return false;
		}
		SiteEntry other = (SiteEntry) object;
		if((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
			return false;
		}
		return true;
	}

	@Override
	public String toString() {
		return this.title != null ? this.title : this.url;
	}
}
